package jicheng;

import java.io.IOException;
import java.util.Arrays;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.xx.jicheng.entity.Category;
import com.xx.jicheng.mapper.CategoryMapper;

public class SpiderCategory {

	public static void main(String[] args) throws IOException {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring.xml");
		CategoryMapper categoryMapper = context.getBean(CategoryMapper.class);
		Document doc = Jsoup.connect("http://www.simall.com.cn/equi/jumpCommodityMain.htm").get();

		Elements elements = doc.select(".item a");
		System.out.println(elements.size());
		for (Element e : elements) {
			String href = e.attr("href").replaceAll("\\s+", "");
			String[] params = href.substring(href.indexOf("?") + 1).split("&");
			System.out.println(Arrays.toString(params));
			String name = e.html();
			String id = params[0].split("=")[1];
			// String pid = params[1].split("=")[1];
			String pid = null;
			if (id.length() == 6) {
				pid = id.substring(0, 4);
			} else if (id.length() == 4) {
				pid = id.substring(0, 2);
			} else {
				pid = "00";
			}
			Category c = new Category();
			c.setId(id);
			c.setName(name);
			c.setPid(pid);
			c.setUrl(href);
			System.out.println(c);
			categoryMapper.save(c);
		}
		context.close();
		/*
		 * insert into dic_category values ('00', '全部', null, null); 
		 * alter table dic_category add constraint Category_PID_FK foreign key (pid) references dic_category;
		 */
	}

}
